package com.example.mutichatkotlin

import android.annotation.SuppressLint
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AbsListView
import android.widget.LinearLayout
import android.widget.TextView

class MsgAdapter(private var mMsgList:List<Msg>): RecyclerView.Adapter<MsgAdapter.ViewHolder>() {


    class ViewHolder(view:View): RecyclerView.ViewHolder(view) {
        val leftLayout=view.findViewById<LinearLayout>(R.id.left_layout)
        val rightLayout=view.findViewById<LinearLayout>(R.id.right_layout)
        val leftMsg=view.findViewById<TextView>(R.id.left_msg)
        val rightMsg=view.findViewById<TextView>(R.id.right_msg)
        val leftUsername=view.findViewById<TextView>(R.id.left_username)
        val rightUsername=view.findViewById<TextView>(R.id.right_username)
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
        val view=LayoutInflater.from(parent?.context).inflate(R.layout.msg_item,parent,false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int {
        return mMsgList.size
    }

    override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
        val msg=mMsgList.get(position)
        if(msg.type==Msg.Properties.TYPE_RECEIVED){
            holder?.leftLayout?.visibility=View.VISIBLE
            holder?.rightLayout?.visibility=View.GONE
            holder?.leftMsg?.text=msg.content
            holder?.leftUsername?.text=msg.username
        }else if(msg.type==Msg.Properties.TYPE_SENT){
            holder?.rightLayout?.visibility=View.VISIBLE
            holder?.leftLayout?.visibility=View.GONE
            holder?.rightMsg?.text=msg.content
            holder?.rightUsername?.text=msg.username
        }
    }

}

